package com.iris.live.services.report.sources;

import com.iris.live.services.common.DateFormatUtils;
import com.iris.live.services.common.GetConfigureInfoUtils;
import com.iris.live.services.common.StringUtils;
import com.iris.live.services.data.repositories.DataCheckRepository;
import com.iris.live.services.report.Cell;
import com.iris.live.services.report.IExcelReportSource;
import com.iris.live.services.report.Row;
import com.iris.live.services.report.Sheet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by lusong on 2017/5/24.
 */
@Component("ExportDataCheckInsuranceContrastReportSource")
public class ExportDataCheckInsuranceContrastReportSource implements IExcelReportSource {

    private Logger logger = LoggerFactory.getLogger(ExportDataCheckInsuranceContrastReportSource.class);

    private String manualData = GetConfigureInfoUtils.getConfigureInfo("file_group") + "_经销商_数据核验_保险_手工台账数据";
    private String systemData = GetConfigureInfoUtils.getConfigureInfo("file_group") + "_经销商_数据核验_保险_LiVE系统数据";

    @Autowired
    private DataCheckRepository dataCheckRepository;

    @Override
    public Sheet[] getSheets(Map<String, Object> context) {
        Sheet[] sheets = new Sheet[2];
        sheets[0] = new Sheet(manualData,manualData,true);
        sheets[1] = new Sheet(systemData,systemData,true);
        return sheets;
    }

    @Override
    public List<Row> getSheetRows(Map<String, Object> context, Sheet sheet) throws ParseException {
        List<Row> values = new ArrayList<>();

        String permit = context.get("permit") == null ? null : context.get("permit").toString();
        String startDate = context.get("startDate") == null ? null : context.get("startDate").toString();
        String endDate = context.get("endDate") == null ? null : context.get("endDate").toString();
        String nowDate = null;
        if (StringUtils.isNullOrBlank(startDate) && StringUtils.isNullOrBlank(endDate)) {
            nowDate = DateFormatUtils.getFormatDateToString(new java.util.Date(), "yyyyMMdd");
        }
        List list = dataCheckRepository.getInsuranceContrast(permit, nowDate, startDate, endDate);

        values.add(new Row());
        values.add(new Row());
        values.add(new Row());
        values.add(new Row());
        values.add(new Row());
        values.add(new Row());

        if (list != null && !list.isEmpty()) {
            for (Object o : list) {
                Map<String, Object> map = (Map) o;
                if (manualData.equals(sheet.getName())) {
                    values.add(new Row(getCells(map)));
                } else if (systemData.equals(sheet.getName())) {
                    values.add(new Row(getOtherCells(map)));
                }
            }
        }
        return values;
    }

    private Cell[] getCells (Map<String, Object> map) {
        Cell[] cells = new Cell[16];
        int index = 0;
        cells[index++] = new Cell(map.get("vin") == null ? "" : map.get("vin").toString(), index, !"02".equals(map.get("compareResult") == null ? null : map.get("compareResult").toString()));
        cells[index++] = new Cell(map.get("importInsuredCustName") == null ? "" : map.get("importInsuredCustName").toString(), index, !"1".equals(map.get("deductionInsuredCustName") == null ? null : map.get("deductionInsuredCustName").toString()));
        cells[index++] = new Cell(map.get("importTciPolicyNum") == null ? "" : map.get("importTciPolicyNum").toString(), index, !"1".equals(map.get("deductionTciPolicyNum") == null ? null : map.get("deductionTciPolicyNum").toString()));
        cells[index++] = new Cell(map.get("importVciPolicyNum") == null ? "" : map.get("importVciPolicyNum").toString(), index, !"1".equals(map.get("deductionVciPolicyNum") == null ? null : map.get("deductionVciPolicyNum").toString()));
        cells[index++] = new Cell(map.get("importTciInvoiceDate") == null ? "" : map.get("importTciInvoiceDate").toString(), index, !"1".equals(map.get("deductionTciInvoiceDate") == null ? null : map.get("deductionTciInvoiceDate").toString()));
        cells[index++] = new Cell(map.get("importVciInvoiceDate") == null ? "" : map.get("importVciInvoiceDate").toString(), index, !"1".equals(map.get("deductionVciInvoiceDate") == null ? null : map.get("deductionVciInvoiceDate").toString()));
        cells[index++] = new Cell(map.get("importTciEffectDate") == null ? "" : map.get("importTciEffectDate").toString(), index, !"1".equals(map.get("deductionTciEffectDate") == null ? null : map.get("deductionTciEffectDate").toString()));
        cells[index++] = new Cell(map.get("importTciExpirationDate") == null ? "" : map.get("importTciExpirationDate").toString(), index, !"1".equals(map.get("deductionTciExpirationDate") == null ? null : map.get("deductionTciExpirationDate").toString()));
        cells[index++] = new Cell(map.get("importVciEffectDate") == null ? "" : map.get("importVciEffectDate").toString(), index, !"1".equals(map.get("deductionVciEffectDate") == null ? null : map.get("deductionVciEffectDate").toString()));
        cells[index++] = new Cell(map.get("importVciExpirationDate") == null ? "" : map.get("importVciExpirationDate").toString(), index, !"1".equals(map.get("deductionVciExpirationDate") == null ? null : map.get("deductionVciExpirationDate").toString()));
        cells[index++] = new Cell(map.get("importTciCompany") == null ? "" : map.get("importTciCompany").toString(), index, !"1".equals(map.get("deductionTciCompany") == null ? null : map.get("deductionTciCompany").toString()));
        cells[index++] = new Cell(map.get("importVciCompany") == null ? "" : map.get("importVciCompany").toString(), index, !"1".equals(map.get("deductionVciCompany") == null ? null : map.get("deductionVciCompany").toString()));
        cells[index++] = new Cell(map.get("importTciPrice") == null ? "" : map.get("importTciPrice").toString(), index, !"1".equals(map.get("deductionTciPrice") == null ? null : map.get("deductionTciPrice").toString()));
        cells[index++] = new Cell(map.get("importVciPrice") == null ? "" : map.get("importVciPrice").toString(), index, !"1".equals(map.get("deductionVciPrice") == null ? null : map.get("deductionVciPrice").toString()));
        cells[index++] = new Cell(map.get("importTciFee") == null ? "" : map.get("importTciFee").toString(), index, !"1".equals(map.get("deductionTciFee") == null ? null : map.get("deductionTciFee").toString()));
        cells[index++] = new Cell(map.get("importVciFee") == null ? "" : map.get("importVciFee").toString(), index, !"1".equals(map.get("deductionVciFee") == null ? null : map.get("deductionVciFee").toString()));
        return cells;
    }


    private Cell[] getOtherCells (Map<String, Object> map) {
        Cell[] cells = new Cell[16];
        int index = 0;
        cells[index++] = new Cell(map.get("vin") == null ? "" : map.get("vin").toString(), index, !"02".equals(map.get("compareResult") == null ? null : map.get("compareResult").toString()));
        cells[index++] = new Cell(map.get("dbInsuredCustName") == null ? "" : map.get("dbInsuredCustName").toString(), index, !"1".equals(map.get("deductionInsuredCustName") == null ? null : map.get("deductionInsuredCustName").toString()));
        cells[index++] = new Cell(map.get("dbTciPolicyNum") == null ? "" : map.get("dbTciPolicyNum").toString(), index, !"1".equals(map.get("deductionTciPolicyNum") == null ? null : map.get("deductionTciPolicyNum").toString()));
        cells[index++] = new Cell(map.get("dbVciPolicyNum") == null ? "" : map.get("dbVciPolicyNum").toString(), index, !"1".equals(map.get("deductionVciPolicyNum") == null ? null : map.get("deductionVciPolicyNum").toString()));
        cells[index++] = new Cell(map.get("dbTciInvoiceDate") == null ? "" : map.get("dbTciInvoiceDate").toString(), index, !"1".equals(map.get("deductionTciInvoiceDate") == null ? null : map.get("deductionTciInvoiceDate").toString()));
        cells[index++] = new Cell(map.get("dbVciInvoiceDate") == null ? "" : map.get("dbVciInvoiceDate").toString(), index, !"1".equals(map.get("deductionVciInvoiceDate") == null ? null : map.get("deductionVciInvoiceDate").toString()));
        cells[index++] = new Cell(map.get("dbTciEffectDate") == null ? "" : map.get("dbTciEffectDate").toString(), index, !"1".equals(map.get("deductionTciEffectDate") == null ? null : map.get("deductionTciEffectDate").toString()));
        cells[index++] = new Cell(map.get("dbTciExpirationDate") == null ? "" : map.get("dbTciExpirationDate").toString(), index, !"1".equals(map.get("deductionTciExpirationDate") == null ? null : map.get("deductionTciExpirationDate").toString()));
        cells[index++] = new Cell(map.get("dbVciEffectDate") == null ? "" : map.get("dbVciEffectDate").toString(), index, !"1".equals(map.get("deductionVciEffectDate") == null ? null : map.get("deductionVciEffectDate").toString()));
        cells[index++] = new Cell(map.get("dbVciExpirationDate") == null ? "" : map.get("dbVciExpirationDate").toString(), index, !"1".equals(map.get("deductionVciExpirationDate") == null ? null : map.get("deductionVciExpirationDate").toString()));
        cells[index++] = new Cell(map.get("dbTciCompany") == null ? "" : map.get("dbTciCompany").toString(), index, !"1".equals(map.get("deductionTciCompany") == null ? null : map.get("deductionTciCompany").toString()));
        cells[index++] = new Cell(map.get("dbVciCompany") == null ? "" : map.get("dbVciCompany").toString(), index, !"1".equals(map.get("deductionVciCompany") == null ? null : map.get("deductionVciCompany").toString()));
        cells[index++] = new Cell(map.get("dbTciPrice") == null ? "" : map.get("dbTciPrice").toString(), index, !"1".equals(map.get("deductionTciPrice") == null ? null : map.get("deductionTciPrice").toString()));
        cells[index++] = new Cell(map.get("dbVciPrice") == null ? "" : map.get("dbVciPrice").toString(), index, !"1".equals(map.get("deductionVciPrice") == null ? null : map.get("deductionVciPrice").toString()));
        cells[index++] = new Cell(map.get("dbTciFee") == null ? "" : map.get("dbTciFee").toString(), index, !"1".equals(map.get("deductionTciFee") == null ? null : map.get("deductionTciFee").toString()));
        cells[index++] = new Cell(map.get("dbVciFee") == null ? "" : map.get("dbVciFee").toString(), index, !"1".equals(map.get("deductionVciFee") == null ? null : map.get("deductionVciFee").toString()));
        return cells;
    }
}
